var api = require("../../api/api.js");
var event = require("../../event/event.js");
var storageManage = require("../../manage/storageManage.js");

const app = getApp();
var ids;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    carts: [],
    cartsShow: false,
    hasList: false,
    totalPrice: 0,
    selectAllStatus: false,
    price: "",
    num: 0

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    event.on('cart', this, function (data) {
      this.updata();
    });
    this.updata();
  },
  /**
   * 
   * 加载购物车数据
   */
  updata: function () {
    // wx.showLoading()
    var that = this;
    this.setData({
      hasList: true,
      carts: [],
      cartsShow: false,
      price: "0",
      selectAllStatus: false
    });
    api.default.api('get_cart_list', {}, that.successCallback, that.errorCallback);

  },
  successCallback: function (e) {
    var list = e.data.cart_list;
    if (list.length <= 0) {
      this.setData({
        cartsShow: true
      })
    }
    var price_ = 0;
    for (var i = 0; i < list.length; i++) {
      price_ += list[i].amount * list[i].price;
    };
    this.setData({
      carts: list,
      totalPrice: price_.toFixed(2)
    });
    wx.hideLoading()

  },
  toDetail: function (e) {
    wx.navigateTo({
      url: '../detail/detail?commodity_id=' + e.currentTarget.id,
    });
  },
  errorCallback: function (e, du) {
    console.log(e);
    this.setData({
      cartsShow: true
    })
    wx.hideLoading()
  },
  /**
   * 单个选中
   */
  setSele: function (e) {
    var index = e.currentTarget.dataset.index;
    console.log(index)
    var listItemStr = "carts[" + index + "].sele";
    this.setData({
      [listItemStr]: !this.data.carts[index].sele
    });
    this.updataPrice();

  },

  /**
   * 全选
   */
  seleAll: function () {

    this.setData({
      selectAllStatus: !this.data.selectAllStatus,

    });
    var list = this.data.carts;
    for (var i = 0; i < list.length; i++) {
      list[i].sele = this.data.selectAllStatus;
    };
    this.setData({
      carts: list
    });
    this.updataPrice();

  },

  //增加
  add: function (e) {
    var index = e.currentTarget.dataset.index;
    var amount = this.data.carts[index].amount;

    var listItemStr = "carts[" + index + "].amount";
    this.setData({
      [listItemStr]: this.data.carts[index].amount += 1
    });

    this.updataPrice();
  },
  //减少
  sub: function (e) {
    var index = e.currentTarget.dataset.index;
    var amount = this.data.carts[index].amount;
    var listItemStr = "carts[" + index + "].amount";
    if (amount <= 1) {
      amount = 1;
    } else {
      amount -= 1;
    }
    this.setData({
      [listItemStr]: amount
    });
    this.updataPrice();

  },
  //结算页
  payFun: function (e) {
    api.default.send_template(e)
    var params = [];
    var specification_ = [];
    for (var i = 0; i < this.data.carts.length; i++) {
      if (this.data.carts[i].sele) {
        var item = {
          "sku_id": this.data.carts[i].sku_id,
          "amount": this.data.carts[i].amount,
          "commodity_id": this.data.carts[i].commodity_id,
        };
        params.push(item);
      }
    }
    if (params.length <= 0) {
      wx.showToast({
        title: '至少选择一件商品',
        icon: 'none'
      })
      return
    }
    console.log("params=" + JSON.stringify(params));
    wx.navigateTo({
      url: '../payinfo/payinfo?params=' + JSON.stringify(params),
    });
  },

  //删除
  dele: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认删除该商品？',
      success: function (res) {
        if (res.confirm) {
          var id = e.currentTarget.id;
          var list = that.data.carts;
          var index = e.currentTarget.dataset.index;
          api.default.api('delete_cart', {
            "sku_id": [id]
          }, function (data) {
            list.splice(index, 1)
            that.setData({
              carts: list
            })
            that.updataPrice()

          }, function (e) {
            console.log(e)
          });

        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  onUnload: function () {
    event.remove('cart', this);
  },


  //更新总价
  updataPrice: function () {
    var list = this.data.carts;
    var price = 0;
    for (var k = 0; k < list.length; k++) {
      if (list[k].sele) {
        price += list[k].amount * list[k].price;
      }
    };
    this.setData({
      totalPrice: price.toFixed(2)
    });

  }

})